<?php

namespace app\admin\controller;

use think\Cache;
use think\Controller;
use think\Exception;
use think\Request;

class Menu extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        return $this->fetch('',['menus'=>$this->menus_tree]);
    }

    /**
     * 添加权限
     *
     * @return \think\Response
     */
    public function add()
    {
        if(\request()->isPost()){
            $pid = input('post.pid',0,'intval');
            $name = input('post.name','','trim');
            $sort = input('post.sort');
            $module = input('post.module','','trim');
            $action = input('post.action','','trim');
            $identification = input('post.identification','','trim');
            $data = [
                'fid' => $pid,
                'name' => $name,
                'sort' => $sort,
                'module' => $module,
                'action' => $action,
                'identification' => $identification,
                'show' => config('code.show_true')
            ];
            $validate = validate('menu');
            if($pid == 0){
                if(!$validate->scene('topadd')->check($data)){
                    $this->result($_SERVER['HTTP_REFERER'],config('code.error'),$validate->getError());
                }else{
                    //顶级权限
                    $id = model('Menu')->add($data);
                    if($id){
                        Cache::store('redis')->rm(config('redis.xm_menus_all'));
                        $this->result($_SERVER['HTTP_REFERER'],config('code.success'),'成功添加');
                    }else{
                        $this->result($_SERVER['HTTP_REFERER'],config('code.error'),'添加失败');
                    }
                }
            }else{
                if(!$validate->scene('add')->check($data)){
                    $this->result($_SERVER['HTTP_REFERER'],config('code.error'),$validate->getError());
                }else{
                    $id = model('Menu')->add($data);
                    if($id){
                        Cache::store('redis')->rm(config('redis.xm_menus_all'));
                        $this->result($_SERVER['HTTP_REFERER'],config('code.success'),'成功添加');
                    }else{
                        $this->result($_SERVER['HTTP_REFERER'],config('code.error'),'添加失败');
                    }
                }
            }
        }else{
            $fid = input('param.fid');
            if(empty($fid)){
                return '参数错误';
            }
            return $this->fetch('',['fid'=>$fid]);
        }
    }

    /**
     * 修改排序序号
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function edit_sort()
    {
        $data = input('post.');
        $validate = validate('menu');
        if(!$validate->scene('edit_sort')->check($data)){
            $this->result($_SERVER['HTTP_REFERER'],config('code.error'),$validate->getError());
        }else{
            try{
                $sort = model('menu')->where(['id'=>$data['id']])->find();
            }catch (Exception $e){
                $this->result($_SERVER['HTTP_REFERER'],config('code.error'),$e->getMessage());
            }
            if($sort->sort == $data['sort']){
                $this->result($_SERVER['HTTP_REFERER'],config('code.error'),'您未做修改哦!');
            }
            try{
                $res = model('menu')->save(['sort'=>$data['sort']],['id'=>$data['id']]);
                if($res){
                    Cache::store('redis')->rm(config('redis.xm_menus_all'));
                    $this->result($_SERVER['HTTP_REFERER'],config('code.success'),'修改成功');
                }else{
                    $this->result($_SERVER['HTTP_REFERER'],config('code.error'),'修改失败');
                }
            }catch (Exception $e){
                $this->result($_SERVER['HTTP_REFERER'],config('code.error'),$e->getMessage());
            }
        }
    }

    /**
     * 修改是否显示
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit_show()
    {
        $show = input('post.');
        $validate = validate('menu');
        if(!$validate->scene('edit_show')->check($show)){
            $this->result($_SERVER['HTTP_REFERER'],config('code.error'),$validate->getError());
        }else{
            try{
                $res = model('menu')->allowField(true)->save(['show'=>$show['show']],['id'=>$show['id']]);
                if($res){
                    Cache::store('redis')->rm(config('redis.xm_menus_all'));
                    $this->result($_SERVER['HTTP_REFERER'],config('code.success'),'修改成功');
                }else{
                    $this->result($_SERVER['HTTP_REFERER'],config('code.error'),'修改失败');
                }
            }catch (Exception $e){
                $this->result($_SERVER['HTTP_REFERER'],config('code.error'),$e->getMessage());
            }
        }
    }

    /**
     * 权限树状结构
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function tree_data()
    {
        $id = input('param.id');
        //有则是管理员权限 无则是套餐权限
        $admin = input('param.admin','');
        //超级管理员全部选中
        if(!empty($id)){
            //普通管理员
            //管理员添加权限
            if(empty($admin)){
                $group = model('AuthorityGroup')->getMealAll($id);
                if(preg_match('/,/',$group['menus'])){
                    $menus = explode(',',$group['menus']);
                    foreach($menus as $k => $v){
                        $this->menus_tree[$v]['checked'] = true;
                    }
                }else{
                    $this->menus_tree[$group['menus']]['checked'] = true;
                }
                //套餐添加权限
            }else{
                $admin = model('admin')->getAdminInfoAll($id);
                if($admin['level'] != 1){
                    if(preg_match('/,/',$admin['menus'])){
                        $menus = explode(',',$admin['menus']);
                        foreach($menus as $k => $v){
                            $this->menus_tree[$v]['checked'] = true;
                        }
                    }else{
                        $this->menus_tree[$admin['menus']]['checked'] = true;
                    }
                }else{
                    foreach($this->menus_tree as $k => $v){
                        $this->menus_tree[$k]['checked'] = true;
                    }
                }
            }
        }
        $data = [];
        $i = 0;
        foreach($this->menus_tree as $k => $v){
            $data[$i]['id'] = $v['id'];
            $data[$i]['pId'] = $v['fid'];
            $data[$i]['name'] = $v['name'];
            $data[$i]['checked'] = !empty($v['checked']) ? $v['checked'] : false;
            $i++;
        }
        return $data;
    }
}
